/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.action;

import com.click.app.model.Delivery;
import com.click.app.service.GenericManager;
import java.util.List;

/**
 *
 * @author orgale
 */
public class DeliveryAction extends BaseAction {

    private GenericManager<Delivery, Long> deliveryManager;
    private List deliverys;
    private Delivery delivery;
    private Long id;

    public void setId(Long id) {
        this.id = id;
    }

    public Delivery getDelivery() {
        return delivery;
    }

    public void setDelivery(Delivery delivery) {
        this.delivery = delivery;
    }

    public String delete() {
        deliveryManager.remove(new Long(delivery.getIdDelivery()));
        saveMessage(getText("delivery.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            delivery = deliveryManager.get(id);
        } else {
            delivery = new Delivery();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (delivery.getIdDelivery() == null);

        delivery = deliveryManager.save(delivery);

        String key = (isNew) ? "delivery.added" : "delivery.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setDeliveryManager(GenericManager<Delivery, Long> deliveryManager) {
        this.deliveryManager = deliveryManager;
    }

    public List getDeliverys() {
        return deliverys;
    }

    public String list() {
        deliverys = deliveryManager.getAll();
        return SUCCESS;
    }
}